package com.whitedust.top10;

import java.util.Random;

/**
 * @author A
 * @since 2025/11/2 21:19
 */

public class tt {
    public int[] sortArray(int[] nums) {
        quis(nums, 0, nums.length - 1);
        return nums;
    }

    public void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

    public void quis(int[] nums, int left, int right) {
        boolean ordered = true;
        for (int i = left; i < right; i++) {
            if (nums[i] > nums[i + 1]) {
                ordered = false;
                break;
            }
        }
        if (ordered) return;
        int p = partition(nums, left, right);
        quis(nums, left, p - 1);
        quis(nums, p + 1, right);
    }

    public int partition(int[] nums, int left, int right) {
        int i = left + new Random().nextInt(right - left + 1);
        int pivot = nums[i];
        swap(nums, left, i);
        int l = left + 1;
        int r = right;
        while (l <= r) {
            while (l <= r && nums[l] <= pivot) l++;
            while (l <= r && nums[r] >= pivot) r--;
            if (l <= r) {
                swap(nums, l, r);
                l++;
                r--;
            }
        }
        swap(nums, left, r);
        return r;
    }
}
